<?php   // $Id: devis_pdf.php 320 2009-01-28 21:51:35Z pcoustillas $ ?>
<?php  
session_cache_limiter('private');
$conf_xml=simplexml_load_file('../include/config.xml');


if ($_GET['pdf_user']=='adm') 
{
	require_once("../include/verif.php");
} else {
	require_once("../client/verif_client.php");
}

$conf_xml=simplexml_load_file('../include/config.xml');


//define('FPDF_FONTPATH','font/');
require_once('mysql_table.php');
require_once("../include/config/common.php");
require_once("../include/config/var.php");
require_once("../include/language/$lang.php");
require_once("entete_doc.php");

$ratio_page=option("devis->nb_ligne_devis"); //nombre de ligne de devis par page
$num_dev=isset($_GET['num_dev'])?$_GET['num_dev']:"";
//$nom=isset($_GET['nom'])?$_POST['nom']:"";
//$devise = ereg_replace('&#128;', $euro, $devise);
//$devise= chr(128);
$devise= '€';
//$devise = "E";
/*$slogan = stripslashes($slogan);
$entrep_nom= stripslashes($entrep_nom);
$social= stripslashes($social);
$tel= stripslashes($tel);
$tva_vend= stripslashes($tva_vend);
$compte= stripslashes($compte);
$reg= stripslashes($reg);
$mail= stripslashes($mail);
*/


//on compte le nombre de ligne
$sql = "SELECT  ${tblpref}cont_dev.num, quanti, uni, article, prix_htva, tot_art_htva FROM ${tblpref}cont_dev RIGHT JOIN ${tblpref}article on ${tblpref}cont_dev.article_num = ${tblpref}article.num WHERE  dev_num = $num_dev";
$req = mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>" . mysql_error());
$nb_li = mysql_num_rows($req);
$nb_pa1 = $nb_li / $ratio_page ;
$nb_pa = ceil($nb_pa1);
if ($nb_pa==0)
{
	$nb_pa=1; //pour afficher les pdf vide
}
$nb_li =$nb_pa * $ratio_page ;

//pour la date
$sql = "SELECT reference,coment, tot_htva, tot_tva, DATE_FORMAT(date,'%d/%m/%Y') AS date_2 from {$tblpref}devis where num_dev = $num_dev";
$req = mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>" . mysql_error());
$data = mysql_fetch_array($req);
$date_dev = $data['date_2'];
$total_htva = $data['tot_htva'];
$total_tva = $data['tot_tva'];
$coment = $data['coment'];
$reference = $data['reference'];
$tot_tva_inc = $total_htva + $total_tva ;

//pour le nom de client
$sql1 = "SELECT mail, nom, nom2, rue, ville, cp, num_tva FROM ${tblpref}client RIGHT JOIN ${tblpref}devis on client_num = num_client WHERE  num_dev = $num_dev";
$req = mysql_query($sql1) or die("Erreur SQL !<br/>$sql1<br/>" . mysql_error());

$data = mysql_fetch_array($req);
$nom = $data['nom'];
$nom2 = $data['nom2'];
$rue = $data['rue'];
$ville = $data['ville'];
$cp = $data['cp'];
$num_tva = $data['num_tva'];
$mail_client = $data['mail'];


//page 1
class PDF extends PDF_MySQL_Table
{
//debut Js
	var $javascript;
    var $n_js;

    function IncludeJS($script) {
        $this->javascript=$script;
    }

    function _putjavascript() {
        $this->_newobj();
        $this->n_js=$this->n;
        $this->_out('<<');
        $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R ]');
        $this->_out('>>');
        $this->_out('endobj');
        $this->_newobj();
        $this->_out('<<');
        $this->_out('/S /JavaScript');
        $this->_out('/JS '.$this->_textstring($this->javascript));
        $this->_out('>>');
        $this->_out('endobj');
    }

    function _putresources() {
        parent::_putresources();
        if (!empty($this->javascript)) {
            $this->_putjavascript();
        }
    }

    function _putcatalog() {
        parent::_putcatalog();
        if (isset($this->javascript)) {
            $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
        }
    }
	
	function AutoPrint($dialog=false, $nb_impr)
	{
    	//Ajoute du JavaScript pour lancer la boîte d'impression ou imprimer immediatement
    	$param=($dialog ? 'true' : 'false');
    	$script=str_repeat("print($param);",$nb_impr);
		
    	$this->IncludeJS($script);
	}
//fin js
}

$pdf=new PDF('p','mm','a4');
$pdf->Open();


for ($i=0;$i<$nb_pa;$i++)
{
	$nb = $i * $ratio_page;
	$num_pa = $i;
	$num_pa2 = $num_pa +1;

	$pdf->AddPage();
	//la grande cellule sous le tableau
	$pdf->SetFillColor(230,245,209);
	$pdf->SetFont('vera','B',6);
	$pdf->SetY(105);
	$pdf->SetX(12);
	$pdf->Cell(186,95,"",1,0,'C',1); //creer la cellule

	//premiere celule le numero de devis
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(85);
	$pdf->SetX(120);
	$pdf->Cell(65,6,"$lang_de_num : $num_dev",1,0,'C',1);

	//deuxieme cellule les coordonées clients
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(27);
	$pdf->SetX(120);
	$pdf->MultiCell(65,6,"$nom \n $nom2 \n $rue \n $cp  $ville \n ",1,'C',1);

	//cellule la tva client
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(70);
	$pdf->SetX(120);
	$pdf->MultiCell(65,6,"$lang_tva: $num_tva",1,'C',1);

	//le logo
	$extention=substr($logo_doc, -3, 3);
	$pdf->Image("../image/$logo_doc",10,8,0, 0,$extention);
	$pdf->ln(20);

	//la date
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(4);
	$pdf->SetX(135);
	$pdf->MultiCell(50,6,"$lang_date: $date_dev",1,'C',1);

	//Troisieme cellule le slogan
/*
	$pdf->SetFont('vera','B',15);
	$pdf->SetY(45);
	$pdf->SetX(10);
	$pdf->MultiCell(71,4,"$slogan",0,'C',0);

	//Vos coordonnées -- intitulé
	$pdf->SetFont('vera','B',8);
	$pdf->SetY(70);
	$pdf->SetX(10);
	$pdf->MultiCell(40,4,"$lang_dev_pdf_soc",1,'R',1);

	//Vos coordonées -- valeur
	$pdf->SetFont('vera','',8);
	$pdf->SetY(70);
	$pdf->SetX(51);
	$pdf->MultiCell(50,4,"$entrep_nom\n$social\n $tel\n $tva_vend \n$compte \n $mail",0,'L',1);
*/

	//$pdf->ln(5);

	insere_entete();
	
	$pdf->SetY(100);

	//Référence
	if ($reference != "")
	{
		$pdf->SetFont('vera','',8);
		//$pdf->SetY(80);
		//$pdf->SetX(40);
		$pdf->Cell(0,4,"Référence : $reference",0,'L',1);
		
	}

	$pdf->ln(5);

	//$pdf->Line(20,65,200,65);
	


	//Le tableau : on définit les colonnes
	$pdf->AddCol('quanti',16,"Qte",'R');
	$pdf->AddCol('uni',15,"$lang_unite",'C');
	$pdf->AddCol('article',70,"$lang_article",'C');
	$pdf->AddCol('taux_tva',15,get_lang("Tx tva"),'R');
	$pdf->AddCol('prix_htva',35,get_lang("Prix U."),'R');
	$pdf->AddCol('tot_art_htva',35,get_lang("Prix HT"),'R');
	$prop=array('HeaderColor'=>array(255,150,100),'color1'=>array(255,255,210),'color2'=>array(230,245,209),'padding'=>2);
	
	$pdf->Table("SELECT ${tblpref}cont_dev.num, quanti, uni, article, taux_tva, prix_htva , tot_art_htva FROM ${tblpref}cont_dev RIGHT JOIN ${tblpref}article on ${tblpref}cont_dev.article_num = ${tblpref}article.num WHERE  dev_num = $num_dev LIMIT $nb, $ratio_page",$prop);

	//les coordonnées vendeurs 2
	/*$pdf->SetFillColor(230,245,209);
	$pdf->SetFont('vera','',8);
	$pdf->SetY(240);
	$pdf->SetX(15);
	$pdf->MultiCell(35,4,"$social\n $tel\n $tva_vend \n$compte \n$reg",0,C,0);*/

	//Pour la signature
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(230);
	$pdf->SetX(60);
	$pdf->MultiCell(40,10,"$lang_po_acc",1,'C',1);

	//Place libre pour la signature
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(230);
	$pdf->SetX(108);
	$pdf->MultiCell(80,10,"\n\n",1,'C',1);

	if($num_pa2 >= $nb_pa)
	{
		//Quatrieme cellule les totaux
		$pdf->SetFont('vera','B',10);
		$pdf->SetY(200);
		$pdf->SetX(158);
		$pdf->MultiCell(40,4,"$total_htva $devise\n $total_tva $devise\n $tot_tva_inc $devise",1,'R',1);
		
		//Cinquieme cellule les enoncés des totaux
		$pdf->SetFont('vera','B',10);
		$pdf->SetY(200);
		$pdf->SetX(100);
		$pdf->MultiCell(48,4,"Total hors tva: \n Total tva: \n Total tva comprise:",1,'R',1);
		$pdf->Line(20,266,200,266);

		//Pour les commentaires
		$pdf->SetFont('vera','',10);
		$pdf->SetY(217);
		$pdf->SetX(10);
		$pdf->MultiCell(190,4,"$coment",0,'C',0);
	}

	//la derniere cellule conditions de facturation
	/*$pdf->SetFont('vera','B',10);
	$pdf->SetY(268);
	$pdf->SetX(30);
	$pdf->MultiCell(160,4,"$lang_condi",0,C,0);*/

	//le nombre de page si necessaire
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(274);
	$pdf->SetX(30);
	$pdf->MultiCell(160,0,"$lang_page $num_pa2 $lang_de $nb_pa\n",0,'R',0);

	$pdf->SetY(267);
	$pdf->SetX(30);
	insere_pied();

}

/*
if (isset($_GET['action']) && $_GET['action']=='mail')
{
	$pdf->AddPage();
	$pdf->SetFont('vera','B',10);
	$pdf->SetY(10);
	$pdf->SetX(30);
	$pdf->MultiCell(160,4,"Conditions génerales de vente\n",0,'C',0);
	$pdf->SetY(70);
	$pdf->SetX(10);
	$pdf->MultiCell(160,4,"$lang_condi_ven",0,'C',0);

}
*/

$file="Devis $num_dev ${nom}.pdf";

if (isset($_GET['action']) && $_GET['action']=='mail')
{
	$from = option("generale->mail");
	$soc = option("generale->nom_societe");

	$pdf->Output($file);
	$to = "$mail_client";
	$sujet = "Devis de $soc";
	$message = "Bonjour, \n\n";
	$message .= "Un devis vous a été adressé par $soc.\n";
	$message .= "Vous le trouverez en pièce jointe de ce mail.\n\n";
	$message .= "Cordialement \n\n $soc";

	require_once("../include/CMailFile.php");
	$newmail = new CMailFile("$sujet","$to","$from","$message","$file","application/pdf");
	$newmail->sendfile();

	echo "<HTML><SCRIPT>document.location='../devis_lister.php';</SCRIPT></HTML>";
	unlink($file);
} else {

   $pdf->Output($file,"I");
	flush();
	
}

?>
